/**
 * 评论审核
 * @author zhengyunfei
 */
var bcSelectData=null;
var mydatagrid=null;
var indexPageViewDialog=null;
var lookContextData=null;
var lookContextDialog="";
layui.use(["form","laypage","layer",'table'],function(){
    var form=layui.form;
    var layer=layui.layer;
    var table = layui.table; //表格
    form.on('select', function(data){
        // console.log(data.elem); //得到select原始DOM对象
        //console.log(data.value); //得到被选中的值]
        // console.log(data.othis); //得到美化后的DOM对象
        form.render('select');
    });


    /**
     *功能:审核通过按钮操作
     * author:zhengyunfei
     * */
    layui.fsButton.test1 = function(elem,data,datagrid){
        mydatagrid=datagrid;
        var flg='Y';
        if(data.length==0){
            layer.alert("请先选择要操作的数据",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        var isExsitsChecked=false;
        var isExsitsNotChecked=false;
        var ids="";
        for(var i=0;i<data.length;i++){
            var isCheck=data[i].answCheckFlg;
            if(isCheck=='Y'){
                isExsitsChecked=true;
            }
            if(isCheck=='N'){
                isExsitsNotChecked=true;
            }
            if(i<data.length-1){
                ids+=data[i].answ_id+",";
            }else{
                ids+=data[i].answ_id;
            }

        }
        if(isExsitsChecked){
            layer.alert("只有审核状态为[待审核][审核不通过]的才能审核通过！",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return;
        }

        if(isExsitsNotChecked){
            layer.confirm('存在上次审核不通过的数据，是否确认审核通过', function(index){
                layer.close(index);
                ajaxSuccess(ids,flg,datagrid);
                return true;
            },function(){
                return ;
            });
            return ;
        }
        ajaxSuccess(ids,flg,datagrid);
    }
    /**
     *功能:审核驳回按钮操作
     * author:zhengyunfei
     * */
    layui.fsButton.test2 = function(elem,data,datagrid){
        mydatagrid=datagrid;
        var flg='N';
        if(data.length==0){
            layer.alert("请先选择要操作的数据",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        var isExsitsChecked=false;
        var isExsitsNotChecked=false;
        var ids="";
        for(var i=0;i<data.length;i++){
            var isCheck=data[i].answCheckFlg;
            if(isCheck=='Y'){
                isExsitsChecked=true;
            }
            if(isCheck=='N'){
                isExsitsNotChecked=true;
            }
            if(i<data.length-1){
                ids+=data[i].answ_id+",";
            }else{
                ids+=data[i].answ_id;
            }

        }
        if(isExsitsNotChecked){
            layer.alert("只有审核状态为[待审核][审核通过]的才能置为审核不通过！",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return;
        }

        if(isExsitsChecked){
            layer.confirm('存在上次审核通过的数据，是否确认更改为审核不通过', function(index){
                layer.close(index);
                ajaxSuccess(ids,flg,datagrid);
                return true;
            },function(){
                return ;
            });
            return ;
        }
        ajaxSuccess(ids,flg,datagrid);
    }
    /**
     *功能:置为首页展示按钮操作
     * author:zhengyunfei
     * */

    layui.fsButton.test3 = function(elem,data,datagrid){
        if(data.length==0){
            layer.alert("请先选择要操作的数据",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        mydatagrid=datagrid;
        var length=data.length;
        if(length>1){
            layer.alert("请选择一条回复记录查看上下文",{
                icon:2,
                skin:'layer-ext-moon'
            });
            return ;
        }
        var video_id=data[0].video_id;
        var answerBoqwe = data[0];
        var answCheckFlg=data[0].answCheckFlg;
        var answCheckFlgName="";
        if(answerBoqwe.answ_check_flg=='Y'){
            answCheckFlgName="审核通过";
        }
        if(answerBoqwe.answ_check_flg=='N'){
            answCheckFlgName="审核不通过";
        }
        if(answerBoqwe.answ_check_flg=='W'){
            answCheckFlgName="待审核";
        }

        $.ajax({
            url: "/videos/oneAnswerList.shtml", //请求的url地址
            dataType: "json", //返回格式为json
            async: true, //请求是否异步，默认为异步，这也是ajax重要特性
            data: {
                "video_id": video_id
            }, //参数值
            type: "POST", //请求方式
            beforeSend: function() {
                //请求前的处理
            },
            success: function(req) {
                //请求成功时处理

                var answDtl=req.answerlist;
                var answList=[];
                var videoBo = answDtl[0].videoBo;
                // var pic = answDtl[0].videoBo.thumb_pic;
                // var videourl = answDtl[0].videoBo.video_link;

                // var videoBo=new Object();
                var videoAnswerBo = new Object();
                videoAnswerBo.answ_check_flg = answerBoqwe.answ_check_flg;
                videoAnswerBo.answ_id = answerBoqwe.answ_id;
                videoAnswerBo.remdFlg=answCheckFlgName;

                for(var i=0;i<answDtl.length;i++){
                    var title=answDtl[i].user_id;
                    var content=answDtl[i].answ_dtl;
                    answList.push({"title":title,"content":content});
                    // var
                    // var valueArray=[];
                    // if(null!=value){
                    //     valueArray= value.split(":");
                    //     if(valueArray.length==2){
                    //         answList.push({
                    //             title:valueArray[0]+":",
                    //             content:valueArray[1]
                    //         })
                    //     }
                    // }

                }
                lookContextDialog=layer.open({
                    type: 1 //此处以iframe举例
                    ,title: '查看上下文'
                    ,area: ['673px', '438px']
                    ,fixed:false
                    ,scrollbar: false
                    /* ,btn: [ '返回']*/
                    /* ,yes: function(){
                         save();
                     }*/
                    ,btn2: function(){
                        layer.close(lookContextDialog);
                    }
                    ,shade: 0
                    ,maxmin: false
                    ,content:$("#lookContextDiv")
                    ,end:function(){
                        location.reload();　　//layer.open关闭刷新
                    }

                });

                $(" .layui-layer-content").css('overflow-x','hidden');
                lookContextData= new Vue({
                    el: '#lookContextAppDiv',
                    data: {
                        answList: answList,
                        videoBo:videoBo,
                        answerBo:videoAnswerBo
                        // answCheckFlg:answCheckFlg
                    }
                })
                alert(videoBo.video_link);
                if ('' != videoBo.video_link) {
                    var c1 = "<video id=\"my-video\" class=\"video-js\" controls preload=\"auto\" width=\"270\" height=\"150\"\n" +
                        "           poster=\"m.jpg\" data-setup=\"{}\">\n" +
                        "        <source id=\"goLook\" src=\"" + videoBo.video_link + "\" type=\"video/mp4\">\n" +
                        "        <!--<source src=\"http://vjs.zencdn.net/v/oceans.webm\" type=\"video/webm\">-->\n" +
                        "        <!--<source src=\"http://vjs.zencdn.net/v/oceans.ogv\" type=\"video/ogg\">-->\n" +
                        "        <p class=\"vjs-no-js\"> To view this video please enable JavaScript, and consider upgrading to a web browser that <a href=\"http://videojs.com/html5-video-support/\" target=\"_blank\">supports HTML5 video</a> </p>\n" +
                        "    </video>";
                    document.getElementById("tbody").innerHTML = c1;
                }

            },
            complete: function() {
                //请求完成的处理
            },
            error: function() {
                //请求出错处理
            }
        });

    }

})
/**
 * 审核操作处理
 * @param id 主键，多个以逗号分隔
 * @param status状态 Y 标识审核通过 N 标识审核驳回
 * @author zhengyunfei
 */
function ajaxSuccess(id,answCheckFlg,datagrid){
    $.ajax({
        url: "/videos/answerCheck.shtml", //请求的url地址
        dataType: "json", //返回格式为json
        async: true, //请求是否异步，默认为异步，这也是ajax重要特性
        data: {
            "answ_id": id,
            "pass_flg":answCheckFlg
        }, //参数值
        type: "POST", //请求方式
        beforeSend: function() {
            //请求前的处理
        },
        success: function(req) {
            //请求成功时处理
            if(datagrid){
                datagrid.reload();
            }else{
                layui.fsDatagrid.datagrid.reload();
            }
            if(lookContextDialog){
                layer.close(lookContextDialog);
            }

        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {
            //请求出错处理
        }
    });
}

/**
 * 保存
 * @author zhengyunfei
 */
function save(){
    var quesIds="";
    var hopgOrders="";
    var isNotSelectHopgOrder=false;
    // 为所有选中的复选框执行函数，
    var length=$("input[name='hopgFlg']:checked").length;
    if(length!=3){
        layer.alert("请选择3个首页展示问题",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $("input[name='hopgFlg']:checked").each(function(index,element){
        var id=$(element).attr('data-id');
        var quesId=$(element).attr('data-quesId');

        var hopgOrder=$("#hopgOrder"+id).val();
        if(''==hopgOrder){
            isNotSelectHopgOrder=true;
        }
        if(index<length-1){
            quesIds+=quesId+",";
            hopgOrders+=hopgOrder+",";
        }else{
            quesIds+=quesId;
            hopgOrders+=hopgOrder;
        }
    })
    if(isNotSelectHopgOrder){
        layer.alert("请为首页展示问题配置顺序",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $.ajax({
        url: "/skQuesDtl/setIndexPageView.shtml", //请求的url地址
        dataType: "json", //返回格式为json
        async: true, //请求是否异步，默认为异步，这也是ajax重要特性
        data: {
            "quesId": quesIds,
            "hopgOrder":hopgOrders
        }, //参数值
        type: "POST", //请求方式
        beforeSend: function() {
            //请求前的处理
        },
        success: function(req) {
            //请求成功时处理
            layer.closeAll();
            mydatagrid.reload();

        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {
            //请求出错处理
        }
    });
}

/**
 * 查看上下文
 * @author zhengyunfei
 */
function preView(){
    var viewData=[];
    var quesIds="";
    var hopgOrders="";
    var isNotSelectHopgOrder=false;
    // 为所有选中的复选框执行函数，
    var length=$("input[name='hopgFlg']:checked").length;
    if(length!=3){
        layer.alert("请选择3个首页展示问题",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    $("input[name='hopgFlg']:checked").each(function(index,element){
        var id=$(element).attr('data-id');
        // var quesId=$(element).attr('data-quesId');
        var quesId=$(element).parent().prev().html();
        var hopgOrder=$("#hopgOrder"+id).val();
        if(''==hopgOrder){
            isNotSelectHopgOrder=true;
        }
        if(index<length-1){
            quesIds+=quesId+",";
            hopgOrders+=hopgOrder+",";
        }else{
            quesIds+=quesId;
            hopgOrders+=hopgOrder;
        }
        viewData.push({
            name:quesId,
            num:hopgOrder
        })
    })
    viewData = viewData.sort(compare("num"));
    if(isNotSelectHopgOrder){
        layer.alert("请为首页展示问题配置顺序",{
            icon:2,
            skin:'layer-ext-moon'
        });
        return ;
    }
    var indexPre=layer.open({
        type: 1 //此处以iframe举例
        ,title: '预览'
        ,area: ['400px', '300px']
        ,btn: ['确认保存', '重新配置']
        ,yes: function(){
            save();
        }
        ,btn2: function(){
            layer.close(indexPre);
        }
        ,shade: 0
        ,maxmin: false
        ,content:$("#preViewDiv")
        ,end:function(){
            layer.close(indexPre);
        }

    });
    new Vue({
        el: '#preViewDiv',
        data: {
            preViewData: viewData
        }
    })
}
// 指定排序的比较函数
function compare(property){
    return function(obj1,obj2){
        var value1 = obj1[property];
        var value2 = obj2[property];
        return value1 - value2;     // 升序
    }
}

